package Demo07;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserRegister extends JFrame implements ActionListener {
    private JPanel mainPanel;//主面板
    private JLabel lblNo,lblName,lblSex,lblAge,lblCls,lblHobby,lblNote;//标签
    private JTextField txtNo,txtName,txtAge;//单行文本框
    private JRadioButton rdoSexMan,rdoSexWoman;//单选框
    private ButtonGroup bg;//组合按钮
    private JComboBox<String> cboCls;//下拉框
    private JCheckBox cboNetwork,cboGame,cboBall,cboBuy;//复选框
    private JTextArea txtNote;//多行文本框
    private JScrollPane scoNote;//滚动面板
    private JButton btnReg,btnExit;
    private String op,oa,ob,oc,od,oe,of,ol,ok,ov;
    //构造方法
    public UserRegister(){
        //实例化控件


        mainPanel = new JPanel(null);
        btnReg = new JButton("注册");
        btnExit = new JButton("退出");
        //
        lblNo = new JLabel("学号");
        lblName = new JLabel("姓名");
        lblSex = new JLabel("性别");
        lblAge = new JLabel("年龄");
        lblCls = new JLabel("班级");
        lblHobby = new JLabel("爱好");
        lblNote = new JLabel("个人简介");
        //
        txtNo = new JTextField();
        txtName = new JTextField();
        txtAge = new JTextField();
        //
        rdoSexMan = new JRadioButton("男");
        rdoSexWoman = new JRadioButton("女");
        //
        bg = new ButtonGroup();
        //
        String[] cls = {"软件19-1","软件19-2","软件19-3","软件19-4","软件19-5","软件19-6"};
        cboCls = new JComboBox<String>(cls);
        //
        cboNetwork = new JCheckBox("上网");
        cboGame = new JCheckBox("游戏");
        cboBall = new JCheckBox("篮球");
        cboBuy = new JCheckBox("购物");
        //
        txtNote = new JTextArea();
        //
        scoNote = new JScrollPane(txtNote);
        //设置控件位置大小
        lblNo.setBounds(30,30,60,25);
        txtNo.setBounds(90,30,150,25);
        lblName.setBounds(30,65,60,25);
        txtName.setBounds(90,65,150,25);
        lblAge.setBounds(30,100,60,25);
        txtAge.setBounds(90,100,150,25);
        lblSex.setBounds(30,135,65,25);
        rdoSexMan.setBounds(90,135,60,25);
        rdoSexWoman.setBounds(180,135,60,25);
        bg.add(rdoSexMan);
        bg.add(rdoSexWoman);//互斥
        lblCls.setBounds(30,170,60,25);
        cboCls.setBounds(90,170,150,25);
        lblHobby.setBounds(30,205,60,25);
        cboBall.setBounds(90,205,60,25);
        cboGame.setBounds(150,205,60,25);
        cboBuy.setBounds(210,205,60,25);
        cboNetwork.setBounds(270,205,60,25);
        lblNote.setBounds(30,240,60,25);
        scoNote.setBounds(90,240,150,100);
        btnReg.setBounds(90,360,60,25);
        btnExit.setBounds(180,360,60,25);
        //
        this.setBounds(100,100,370,450);
        //将控件放入容器
        setContentPane(mainPanel);
        mainPanel.add(lblName);
        mainPanel.add(lblAge);
        mainPanel.add(lblCls);
        mainPanel.add(lblHobby);
        mainPanel.add(lblNote);
        mainPanel.add(lblNo);
        mainPanel.add(lblSex);
        mainPanel.add(txtAge);
        mainPanel.add(txtName);
        mainPanel.add(txtNo);
        mainPanel.add(txtNote);
        mainPanel.add(scoNote);
        mainPanel.add(rdoSexMan);
        mainPanel.add(rdoSexWoman);
        mainPanel.add(cboCls);
        mainPanel.add(cboBall);
        mainPanel.add(cboBuy);
        mainPanel.add(cboGame);
        mainPanel.add(cboNetwork);
        mainPanel.add(btnReg);
        mainPanel.add(btnExit);

        btnExit.addActionListener(this);
        btnReg.addActionListener(this);

        //设置窗口可见
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

    }


    public static void main(String[] args) {
        new UserRegister();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(btnExit)){
            System.exit(0);
        }
        if(e.getSource().equals(btnReg)){
            op = String.valueOf(txtNo.getText());
            oa = String.valueOf(txtName.getText());
            ob = String.valueOf(txtAge.getText());
            od = String.valueOf(cboCls.getSelectedItem());
            if(rdoSexMan.isSelected()){
                ol = "男";
            }else{
                ol = "女";
            }
            if(cboNetwork.isSelected()){
                oc = "上网";
            }else {
                oc = "";
            }
            if (cboGame.isSelected()){
                ok = "游戏";
            }else {
                ok = "";
            }
            if(cboBall.isSelected()){
                of = "篮球";
            }else {
                of = "";
            }
            if (cboBuy.isSelected()){
                ov = "购物";
            }else {
                ov = "";
            }
            oe = oc + ok + of + ov;
            JOptionPane.showMessageDialog(null,op + "\n" + oa + "\n" + ob + "\n" + ol + "\n" +od + "\n" + oe);
        }
    }
}

